package cn.com.callback

import cn.com.elite.EliteBean
import cn.com.elite.Util
import cn.com.kh.KHDBConfig
import cn.com.kh.KHUtil
import net.sf.json.JSONObject
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.jdbc.core.JdbcTemplate

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

public class apiChatRefresh implements ServiceBackInvoke {
    private static final Logger log = LoggerFactory.getLogger(apiChatRefresh.class);
    private static long startTime = System.currentTimeMillis();

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");

        // 获取数据源
        JdbcTemplate jt = new JdbcTemplate(eliteBean.getDataSource());

        // 返回对象
        JSONObject result = new JSONObject();

        // 调用IP过滤
        if (KHDBConfig.getParam(eliteBean, "#APIIP") == "1" && !KHUtil.getRemoteHost(request)) {
            log.warn("API被非法IP调用-Request Not Allow!");
            return "Request Not Allow!";
        }

        String getMethod = request.getMethod();
        log.debug("getMethod:" + getMethod);

        try {
            String requestType = request.getParameter("requestType");
            String userId = request.getParameter("userId");
            String token = request.getParameter("token");

            // 拼接获取各类调用必填参数信息
            String isCheckNull = KHUtil.isNullMSG("requestType", requestType) + KHUtil.isNullMSG("userId", userId);

            // 如果必填参数信息不为空说明有为空参数
            if (!KHUtil.isNull(isCheckNull)) {
                result.put("code", 0);
                result.put("msg", "调用参数非空校验异常:" + isCheckNull + "，调用处理时长(毫秒):" + (System.currentTimeMillis() - startTime));
            } else {
                String SYS_InnerDomain = KHDBConfig.getAPIURL(eliteBean, "SYS_InnerDomain");
                String SYS_InnerDomain_chat = KHDBConfig.getAPIURL(eliteBean, "SYS_InnerDomain_chat");

                if (KHUtil.isNull(token)) {
                    String ngsTokenURL = SYS_InnerDomain + "/EliteGeneralHttpTask/GeneralService/apiNGSToken";
                    log.debug("调用ngsTokenURL:" + ngsTokenURL);

                    JSONObject apiTokenJson = JSONObject.fromObject(Util.doHttpGet(ngsTokenURL, "UTF-8", 5000, 0));
                    log.debug("调用ngsTokenURL返回:" + apiTokenJson.toString());

                    if (KHUtil.GetSFJV(apiTokenJson, "code") == "1") {
                        String access_token = apiTokenJson.getJSONObject("data").getString("access_token");

                        String refreshPropertyResult = chatRefresh(SYS_InnerDomain_chat, access_token, requestType, userId);
                        result.put("code", 1);
                        result.put("msg", "处理成功(统一Token)");
                        result.put("data", refreshPropertyResult);

                    } else {
                        result.put("code", -1);
                        result.put("msg", KHUtil.GetSFJV(apiTokenJson, "msg"));
                    }
                } else {
                    String refreshPropertyResult = chatRefresh(SYS_InnerDomain_chat, token, requestType, userId);
                    result.put("code", 1);
                    result.put("msg", "处理成功(界面Token)");
                    result.put("data", refreshPropertyResult);

                }
            }

        } catch (Exception e) {
            log.error("接口处理异常", e);
            result.put("code", -1);
            result.put("msg", e.getMessage());

        }

        log.debug("Token处理完成返回结果:" + result.toString());

        return result.toString();
    }

    public static String chatRefresh(String SYS_InnerDomain_chat, String access_token, String requestType, String userId) {

        String refreshPropertyURL = SYS_InnerDomain_chat + "/webchat/ahr.do?token=" + access_token + "&requestType=" + requestType + "&userId=" + userId;
        log.debug("调用网聊接口URL : " + refreshPropertyURL);

        String refreshPropertyResult = Util.doHttpPost(refreshPropertyURL, "", "UTF-8", 10000, 0);
        log.debug("调用网聊刷新返回 : " + refreshPropertyResult);

        return refreshPropertyResult;
    }
}